﻿using UnityEngine;
using UnityEditor;
using System.Text;
using System.IO;

namespace Cwl.Fromwork.Editor
{

    /// <summary>
    /// 控制器
    /// <summary>
    public class EncodeConverEditor
    {

        //创建UTF-8代码
        [MenuItem("Assets/cwl/Create Script UTF-8", false, 10)]
        static void CreateCodeText()
        {
            // 获取选中的文件夹或文件的路径
            string selectedPath = AssetDatabase.GetAssetPath(Selection.activeObject);

            string className = "NewBehaviourScript";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(string.Format("using UnityEngine;"));
            stringBuilder.AppendLine();
            stringBuilder.AppendLine(string.Format("/// <summary>"));
            stringBuilder.AppendLine(string.Format("/// 控制器"));
            stringBuilder.AppendLine(string.Format("/// <summary>"));
            stringBuilder.AppendLine(string.Format("public class {0} : MonoBehaviour", className));
            stringBuilder.AppendLine("{");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("}");
            string codeText = stringBuilder.ToString();

            //string codeText = "using UnityEngine;\r\n\r\npublic class NewBehaviourScript1 : MonoBehaviour\r\n{\r\n\r\n}\r\n";

            File.WriteAllText(selectedPath + $"/{className}.cs", codeText, System.Text.Encoding.UTF8);

            // 刷新Unity资源窗口
            AssetDatabase.Refresh();
        }


        [MenuItem("Assets/cwl/TO UTF8")]
        static void TOutf8()
        {
            Caching.ClearCache();
            Object obj = Selection.activeObject;
            string rootPath = AssetDatabase.GetAssetPath(obj);


            if (obj != null && obj is TextAsset)
            {
                Toutf8(System.Environment.CurrentDirectory + "/" + rootPath);
            }
            else
            {
                //string filter = "t:Scene t:Prefab t:Texture";
                string filter = "t:Script";
                string[] resFiles = AssetDatabase.FindAssets(filter, new string[] { rootPath });

                for (int i = 0; i < resFiles.Length; i++)
                {
                    //"Assets/Resources/Cube (1).prefab"
                    resFiles[i] = AssetDatabase.GUIDToAssetPath(resFiles[i]);
                    //"Cube (1)" 
                    string fileName = System.IO.Path.GetFileNameWithoutExtension(resFiles[i]);

                    string path = resFiles[i];
                    Toutf8(System.Environment.CurrentDirectory + "/" + path);
                }
            }

            Debug.Log("查找to utf-8 结束 ");
            AssetDatabase.Refresh();
        }


        private static void Toutf8(string assetPath)
        {
            Encoding encoding = Encoding.GetEncoding(936); //ANSI
            string text = File.ReadAllText(assetPath, encoding);

            System.IO.File.WriteAllText(assetPath, text, Encoding.UTF8);

        }

    }

}